
#include "Parser.h"

#include<fstream>

using namespace std;

//*****************************************************************************
//Constructor from file name
//*****************************************************************************
Parser::Parser(const char* fn)
{
	numOfLines = 0;
	fileName = fn;
}

//*****************************************************************************
//copy CTOR
//*****************************************************************************
Parser::Parser(const Parser& prs)
{
	numOfLines = prs.numOfLines;
	fileName = prs.fileName;
	fileLines = prs.fileLines;
}

//*************************************************************************
//assignment operator
//*************************************************************************
const Parser& Parser::operator=(const Parser& prs)
{
	if (this != &prs)
	{
		numOfLines = prs.numOfLines;
		fileName = prs.fileName;
		fileLines = prs.fileLines;
	}

	return *this;
}

//*****************************************************************************
//divide file to lines and store them in list of strings
//return 1 if succeeded, 0 otherwise
//*****************************************************************************
int Parser::divideToLines()
{
	string line;
	ifstream myfile (fileName);
	
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			getline (myfile,line);
			fileLines.push_back(line);
			numOfLines++;
		}
		myfile.close();
	}
	else 
	{
		cout << "ERROR: Unable to open file"; 
		return 0;
	}

	return 1;
}

//*************************************************************************
//get reference to container of file lines
//*************************************************************************
const vector<string>& Parser::getLineContainer() const
{
	return fileLines;
}

//*****************************************************************************
//get the number of lines in file
//*****************************************************************************
int Parser::getNumOfLines() const
{
	return numOfLines;
}

//*****************************************************************************
//printout lines of file
//*****************************************************************************
void Parser::printOutLines() const
{
	vector<string>::const_iterator it;
	it = fileLines.begin();
	while (it != fileLines.end())
		cout << *(it++) << endl;
}

//*****************************************************************************
//DTOR
//*****************************************************************************
Parser::~Parser(void){}